package com.ruoyi.game.service;

import com.ruoyi.game.constant.GameRoomProperties;
import com.ruoyi.game.core.GameRoomCore;
import com.ruoyi.game.service.impl.GameRoomServiceImpl;
import com.ruoyi.game.websocket.handler.GameWebSocketHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Data
@Component
public class GameRoomMemoryManager {

    /** 房间内存缓存 */
    private final Map<Long, GameRoomCore> roomMap = new ConcurrentHashMap<>();

    public GameRoomCore create(Long dbId, String roomKey,
                               GameWebSocketHandler socketHandler,
                               IGameRoomService roomService,
                               IGameSetService gameSetService,
                               IGamePlayerService gamePlayerService,
                               GameRoomProperties gameRoomProperties,
                               boolean robotRoom) {
        GameRoomCore core = new GameRoomCore(dbId, roomKey, socketHandler, roomService, gameSetService, gamePlayerService, gameRoomProperties, robotRoom);
        roomMap.put(dbId, core);
        return core;
    }

    public GameRoomCore get(Long roomId) {
        return roomMap.get(roomId);
    }

    public void remove(Long roomId) {
        roomMap.remove(roomId);
    }

    public Collection<GameRoomCore> all() {
        return roomMap.values();
    }
}
